package com.simpletour.qa.common.entity;

import com.simpletour.qa.common.repository.LocalDateTimeAttributeConverter;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Set;


/**
 * 用户
 * @author zhaohw
 * @date 2018-06-08 16:56
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(schema = "qa" , name="t_user")
public class User implements Serializable {

    /**
     * 用户正常状态
     */
    public final static Integer USER_STATUS_NORMAL = 0;

    /**
     * 用户禁用状态
     */
    public final static Integer USER_STATUS_FORBIDDEN = 1;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name="user_id")
    private Integer userId;

    @NotBlank
    private String username;

    @NotBlank
    private String password;

    private String nickname;

    @Column(name = "mobile_phone")
    private String mobilePhone;

    private String position;

    private String avatar;

    @Convert(converter = LocalDateTimeAttributeConverter.class)
    @Column(name = "create_time")
    private LocalDateTime createTime;

    @ManyToMany(cascade = CascadeType.PERSIST,fetch =FetchType.LAZY)
    @JoinTable(name = "qa.t_user_ref_role",
            joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "user_id")},
            inverseJoinColumns = {@JoinColumn(name = "role_id", referencedColumnName = "role_id")})
    private Set<Role> roles;

    private String gender;

    private String email;

    private Integer status ;

    /**
     * 用户初始化
     * 包含user的状态
     * @return
     */
    public void init(){
        this.status = USER_STATUS_NORMAL;
    }
}
